﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmTaskFilter : FrmBase
    {
        private PlanService planService;
        private string parentForm = "";
        private ScanService scanService;

        public FrmTaskFilter()
        {
            InitializeComponent();
            ttlbrhead.Text = "任务过滤";
            ilstbcup.Images.Add(ResourceImage.确定01);
            ilstbcup.Images.Add(ResourceImage.键盘01);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.确定02);
            ilstdown.Images.Add(ResourceImage.键盘02);
            ilstdown.Images.Add(ResourceImage.返回02);
            btnSelectDeviceType.UpImage = ResourceImage.设备类型01;
            btnSelectDeviceType.DownImage = ResourceImage.设备类型02;
            ibtnscanner.UpImage = ResourceImage.使用扫描仪01;
            ibtnscanner.DownImage = ResourceImage.使用扫描仪02;

            
        }

        private void FrmTaskFilter_Load(object sender, EventArgs e)
        {
            scanService = new ScanService(this);
            scanService.OnReadCode += scanService_OnReadCode;

            planService = new PlanService();

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();
        }


        private void FrmTaskFilter_Closed(object sender, EventArgs e)
        {
            scanService.Close();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (Parameter.Count == 1)
            {
                parentForm = (string)Parameter[0];
            }
            else
            {
                tbDeviceType.Text = (string)Parameter[0];

                //如果是从设备类型选择返回，需要读取之前的信息
                object[] oldParameters = (object[])(Parameter[1]);
                parentForm = (string)oldParameters[0];
                tbDeviceName.Text = (string)oldParameters[1];
                tbDeviceCode.Text = (string)oldParameters[3];
            }
        }

        private void LoadCacheData()
        {
            parentForm = Caches[0].ToString();
            tbDeviceCode.Text = Caches[1].ToString();
            tbDeviceName.Text = Caches[2].ToString();
            tbDeviceType.Text = Caches[3].ToString();
        }

        void scanService_OnReadCode(string code)
        {
            tbDeviceCode.Text = code;
            QueryPlanByDevice();
        }

        private void bcxunjian_OnButtonClick_1(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:
                    QueryPlanByDevice();
                    break;
                case 1:
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbDeviceName.Focus();
                    break;
                case 2:
                    FormManage.GotoNext(parentForm, null, null, true);
                    break;

            }
        }

        private void QueryPlanByDevice()
        {
            List<Plan> plans = planService.GetPlansByDevice(AppLogic.LoginUser.UserName, tbDeviceCode.Text, tbDeviceName.Text, tbDeviceType.Text);
            FormManage.GotoNext(parentForm, new object[] { plans },null, false);
        }

        private void btnSelectDeviceType_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmEquipLst", new object[] { "FrmTaskFilter", new object[] { parentForm, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text } }, new object[] { parentForm, tbDeviceName.Text, tbDeviceType.Text, tbDeviceCode.Text }, false);
        }


        private void ibtnscanner_Click(object sender, EventArgs e)
        {
            scanService.Scan();
        }

        private void FrmTaskFilter_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "194")
            {
                scanService.Scan();
            }
        }


    }
}